<?php

namespace backend\modules\classify\controllers;

use common\models\firstpage\Address;
use common\models\user\Member;
use Yii;
use common\enums\StatusEnum;
use common\traits\Curd;
use common\models\base\SearchModel;
use common\models\classify\Order;
use backend\controllers\BaseController;

/**
 * Class NotifyAnnounceController
 * @package backend\modules\base\controllers
 * @author jianyan74 <751393839@qq.com>
 * 订单管理
 */
class OrderController extends BaseController
{
    use Curd;

    /**
     * @var Order
     */
    public $modelClass = Order::class;

    /**
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['order_number'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC,
            ],
            'pageSize' => $this->pageSize,
        ]);
        $hu =self::actionHu();
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
       // var_dump(\Qiniu\json_decode($dataProvider,true));die();
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'menuDropDownList' =>['0'=>'已取消','1'=>'未接单','2'=>'已接单','3'=>'已完成'],
        ]);
    }

    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
//        $order_address = Yii::$app->request->get('order_address');
//       if ($order_address){
//           $data =self::actionAddress($order_address);
//       }
        if ($id){
            $data = Order::findOne($id);
           // var_dump($data['order_name']);die();
            $arr = self::actionMbr($data['order_name']);
        }
        $model = $this->findModel($id);
        if($id){
            $data = Order::findOne($id);
            $res = self::actionAddres($data['order_address']);
        }
        $model-> province_id = $res['province_id'];
        $model-> city_id = $res['city_id'];
        $model-> area_id = $res['area_id'];
        $model->img = explode(',',$model->img);//
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');//随机订单编号
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        $model->order_number =$orderSn;
        if ($model->load(Yii::$app->request->post())) {
            $arr = implode(',',$model['img']);
            $model->img = $arr;
            if ($model->save()){
                return $this->redirect(['index']);
            }

        }
        return $this->render($this->action->id, [
            'model' => $model,
            'data'=>$arr,
            'res'=>$res,

        ]);
    }

    /**
     * @param $id
     * @return array|\yii\db\ActiveRecord|null
     *
     */

    public static function actionMbr($id){

        $data = Member::find()
            ->where(['id'=>$id])
            ->select('member_name')
            ->asArray()
            ->one();

        return $data;
    }

public static function actionAddres($id){
        $res = Address::find()
            ->where(['id'=>$id])
            ->select(['address','phone','province_id','city_id','area_id'])
            ->asArray()
            ->one();
       // var_dump($res);die();
        return $res;
}
public function actionAtt(){
        $id =Yii::$app->request->get();
        if ($id){
            return $this->message("不能重复捐赠", $this->redirect(Yii::$app->request->referrer), 'error');
        }
}

public static function actionHu(){
    $data = Order::find()
        ->where(['news'=>1])
        ->select('id,news')
        ->asArray()
        ->all();
     $arr = array_column($data,'id');
    $a = Order::updateAll(['news'=>2],['in','id',$arr]);
    return $a;
}

}

